package com.wanmait.hotelManage.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.PagerDAO;
import com.wanmait.hotelManage.dao.RoomDAO;
import com.wanmait.hotelManage.dao.RoomTypeCommentDAO;
import com.wanmait.hotelManage.dao.RoomTypeCommentReportDAO;
import com.wanmait.hotelManage.vo.Pager;
import com.wanmait.hotelManage.vo.Room;
import com.wanmait.hotelManage.vo.RoomTypeComment;
import com.wanmait.hotelManage.vo.RoomTypeCommentReport;


/**
 * Servlet implementation class RoomTypeCommentReport
 */
@WebServlet("/manage/RoomTypeCommentReportServlet")
public class RoomTypeCommentReportServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action=request.getParameter("action");
		if("init".equals(action)) {
			this.init(request, response);
		}else if("updateby".equals(action)) {
			this.updateby(request, response);
		}else if("updateNoby".equals(action)) {
			this.updateNoby(request, response);
		}else if("updaeups".equals(action)) {
			this.updaeups(request, response);
		}else if("updaeNoups".equals(action)) {
			this.updaeNoups(request, response);
		}else if("initTwo".equals(action)) {
			this.initTwo(request, response);
		}else if("search".equals(action)) {
			this.search(request, response);
		}else if("searchTwo".equals(action)) {
			this.searchTwo(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	/**
	 * 搜索分页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void searchTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer pageNo=Integer.parseInt(request.getParameter("id"));
		String text=request.getParameter("text");
		Pager pager=new Pager();
		pager.setPageNo(pageNo);
		pager.setPageSize(4);
		int rows=RoomTypeCommentReportDAO.getRoomtypecommentreportdao().getrowss(text);
		pager.setPageCount(rows);
		List<RoomTypeCommentReport>roomTypeCommentReports=RoomTypeCommentReportDAO.getRoomtypecommentreportdao().findname(text,pager);
		if(roomTypeCommentReports!=null) {
			List<Integer>a=new ArrayList<Integer>();
			for (RoomTypeCommentReport roomTypeCommentReport : roomTypeCommentReports) {
				a.add(roomTypeCommentReport.getUserinfo().getId());
			}
			String id=a.toString();
			String id1=id.substring(1,id.length()-1);
//			System.out.println(id1);
		List<RoomTypeComment>roomTypeComments=RoomTypeCommentDAO.getRoomtypecommentdao().findid1(id1,pager);
		request.setAttribute("roomTypeCommentReports", roomTypeCommentReports);
		request.setAttribute("roomTypeComments", roomTypeComments);
		request.setAttribute("pager", pager);
		request.setAttribute("text", text);
		request.getRequestDispatcher("/manage/roomTypeCommentReport/searchTwo.jsp").forward(request, response);
		}
	}
	/**
	 * 搜索
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String text=request.getParameter("text");
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(4);
		int rows=RoomTypeCommentReportDAO.getRoomtypecommentreportdao().getrowss(text);
		pager.setPageCount(rows);
		List<RoomTypeCommentReport>roomTypeCommentReports=RoomTypeCommentReportDAO.getRoomtypecommentreportdao().findname(text,pager);
		if(roomTypeCommentReports!=null) {
			List<Integer>a=new ArrayList<Integer>();
			for (RoomTypeCommentReport roomTypeCommentReport : roomTypeCommentReports) {
				a.add(roomTypeCommentReport.getUserinfo().getId());
			}
			String id=a.toString();
			String id1=id.substring(1,id.length()-1);
//			System.out.println(id1);
		List<RoomTypeComment>roomTypeComments=RoomTypeCommentDAO.getRoomtypecommentdao().findid1(id1,pager);
		request.setAttribute("roomTypeCommentReports", roomTypeCommentReports);
		request.setAttribute("roomTypeComments", roomTypeComments);
		request.setAttribute("pager", pager);
		request.setAttribute("text", text);
		request.getRequestDispatcher("/manage/roomTypeCommentReport/search.jsp").forward(request, response);
		}
	}
	/**
	 * 分页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void initTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer pageNo=Integer.parseInt(request.getParameter("id"));
		System.out.println(2);
		Pager pager=new Pager();
		pager.setPageNo(pageNo);
		pager.setPageSize(4);
		Integer rows=RoomTypeCommentReportDAO.getRoomtypecommentreportdao().getrows();
		pager.setPageCount(rows);
		List<RoomTypeCommentReport>RoomTypeCommentReports=RoomTypeCommentReportDAO.getRoomtypecommentreportdao().findall(pager);
		List<RoomTypeComment>RoomTypeComments=RoomTypeCommentDAO.getRoomtypecommentdao().findAllFromRoomTypeCommentReport(pager);
		request.setAttribute("RoomTypeCommentReports", RoomTypeCommentReports);
		request.setAttribute("RoomTypeComments", RoomTypeComments);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/roomTypeCommentReport/initTwo.jsp").forward(request, response);
	}
	/**
	 * 批量打回
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void updaeNoups(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String ids=request.getParameter("ids");
		RoomTypeCommentReportDAO.getRoomtypecommentreportdao().updateNObys(ids);
	}
	/**
	 * ajax的批量通过
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void updaeups(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 String ids=request.getParameter("ids");
	 RoomTypeCommentReportDAO.getRoomtypecommentreportdao().updatebys(ids);
	 List<Integer>integer1=RoomTypeCommentReportDAO.getRoomtypecommentreportdao().findRoomTypeCommentId(ids);
	 String id=integer1.toString();
	 String id1=id.substring(1,id.length()-1);
	 RoomTypeCommentDAO.getRoomtypecommentdao().updatebys(id1);
	 RoomTypeCommentReportDAO.getRoomtypecommentreportdao().updateoks(ids);
	}
	/**
	 * ajax的被打回的操作
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void updateNoby(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		RoomTypeCommentReportDAO.getRoomtypecommentreportdao().updateNoby(Integer.parseInt(id));
	}
	/**
	 * 通过
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void updateby(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		String id1=request.getParameter("id1");
		RoomTypeCommentReportDAO.getRoomtypecommentreportdao().updateby(Integer.parseInt(id));
		RoomTypeCommentDAO.getRoomtypecommentdao().updateby(Integer.parseInt(id1));
		System.out.println(id1);
		RoomTypeCommentReportDAO.getRoomtypecommentreportdao().updateok(Integer.parseInt(id));
		response.sendRedirect(request.getContextPath()+"/manage/RoomTypeCommentReportServlet?action=init");
	}
	protected void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager=new Pager();
		pager.setPageSize(4);
		pager.setPageNo(1);
		Integer rows=RoomTypeCommentReportDAO.getRoomtypecommentreportdao().getrows();
		pager.setPageCount(rows);
		List<RoomTypeCommentReport>RoomTypeCommentReports=RoomTypeCommentReportDAO.getRoomtypecommentreportdao().findall(pager);
		List<RoomTypeComment>RoomTypeComments=RoomTypeCommentDAO.getRoomtypecommentdao().findAllFromRoomTypeCommentReport(pager);
		request.setAttribute("RoomTypeCommentReports", RoomTypeCommentReports);
		request.setAttribute("RoomTypeComments", RoomTypeComments);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/roomTypeCommentReport/list.jsp").forward(request, response);
	}
}
